<?php
/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/7
 * Time: 下午4:48
 */
class Budget_model extends CI_Model
{

    public function __construct(){
        $this->load->database();
        $this->load->helper("common");
    }

    //每月额度预算信息
    public function query_budget_info($month,$customer_id)
    {
        $sql = "select customer_id,budget,used from app_budget WHERE month = '$month' and customer_id = '$customer_id' ";
        $res = $this->db->query($sql)->row_array();
        if(!$res)
        {
            $res['budget'] = 0;
            $res['used'] = 0;
            $res['month'] = $month;
        }
        return $res;
    }

    //修改预算通知上级
    public function total_message_history($customer_id,$data){
        $query = $this->db->query("SELECT parent_id ,username FROM app_customer WHERE id=" . $customer_id);
        $r_info = $query->row_array();
        $datas['send_customer_id'] = $customer_id;
        $datas['title'] = '可用预算申请';
        $datas['created_at'] = date('Y-m-d H:i:s', time());
        $datas['content'] = $r_info['username'] . '的可用预算申请，期望额度为￥' . $data['expect_budget'] . '元;备注:' . $data['remark'];
        $datas['receiver_customer_id'] = $r_info['parent_id'];
        $datas['read_status'] = 0;
        $history = $this->db->insert('message_history', $datas);
        return $history;
    }
    //修改单品预算通知上级
    public function singel_message_history($customer_id,$data){
        $query = $this->db->query("SELECT parent_id ,username FROM app_customer WHERE id=" . $customer_id);
        $r_info = $query->row_array();
        $datas['send_customer_id'] = $r_info['parent_id'];
        $datas['title'] = '单品额度申请';
        $datas['created_at'] = date('Y-m-d H:i:s', time());;
        $datas['content'] = $r_info['username'] . '的单品额度申请，期望金额为￥' . $data['expect_single_quote'] . '元;备注:' . $data['remark'];
        $datas['receiver_customer_id'] = $customer_id;
        $datas['read_status'] = 0;
        $history = $this->db->insert('message_history', $datas);
        return $history;
    }
    //单品额度和预算申请通知上级
    public function singel_total_message_history($customer_id,$data,$data1){
        $query = $this->db->query("SELECT parent_id ,username FROM app_customer WHERE id=" . $customer_id);
        $r_info = $query->row_array();
        $datas['send_customer_id'] = $customer_id;
        $datas['title'] = '单品额度和预算申请';
        $datas['created_at'] = date('Y-m-d H:i:s', time());;
        $datas['content'] = $r_info['username'] . '的可用预算申请，期望额度为￥' . $data['expect_budget'] . '元;单品额度申请，期望金额为￥' . $data1['expect_single_quote'] . '元;备注:' . $data['remark'];
        $datas['receiver_customer_id'] = $r_info['parent_id'];
        $datas['read_status'] = 0;
        $history = $this->db->insert('message_history', $datas);
        return  $history;
    }


}